#coding=utf-8
#LightCam 进程监控程序
#init by hfh 20150212

import multiprocessing,subprocess,threading
import socket,os,time,re,traceback,sys
import lc_libs

def func(this):
	#agent_hostname=this['agent_hostname']
	AlertGroup=this['alertgroup']
	procname=this['procname']
	exceptword=this['exceptword']
	exactly=this['exactly']
	definenumber=this['definenumber']
	operator=this['operator']
	interval=this['interval']
	severity=this['severity']
	schedule=this['schedule']
	holiday=this['holiday']
	try:
		Node=agent_hostname
		#AlertGroup="MonitorProcess"
		AlertKey=procname
		Summary=""

		#初始化标志文件
		alertfilename=lchome+"/alert/lc_process_"+procname
		
		#comd="ps -elf|grep -w "+str(procname)+"|grep -v grep|wc -l"
		comd=""
		if int(exactly)==1:
			comd='''ps -elf|grep -w "%s"|egrep -v "%s"|wc -l''' % (procname,exceptword)
		else:
			comd='''ps -elf|grep "%s"|egrep -v "%s"|wc -l''' % (procname,exceptword)

		#执行进程监控
		#comd="ps -elf|grep -w "+str(procname)+"|grep -v grep|wc -l"
		popen=subprocess.Popen(comd,stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True)
		#判断子进程是否结束
		#while True:
		#	if popen.poll()==None:
		#		time.sleep(0.1)
		#		continue
		#	else:
		#		break
		#处理程序
		#line=popen.stdout.read().strip("\n").strip("\r")
		line=popen.stdout.read().strip("\r\n ")
		res=lc_libs.lc_operator(int(line),operator,int(definenumber))
		#print "OLD RES:%s\n" % res
		if not res:
			time.sleep(1)
			popen=subprocess.Popen(comd,stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True)
			line=popen.stdout.read().strip("\r\n ")
			res=lc_libs.lc_operator(int(line),operator,int(definenumber))
			#print "NEW RES:%s\n" % res
		if res:
			Severity="1"
			Type="2"
			#Summary="DefineCount:"+definenumber+",RealCount:"+line
			Summary=agent_hostname+"进程"+procname+"实例数量正常,应该为"+definenumber+",实际为"+line
			strs=lc_libs.lc_gen_alert(agent_hostname,Node,AlertGroup,AlertKey,Severity,Type,Summary)
			print strs
			db.update_eventsdb(events_table,agent_hostname,Node,AlertGroup,AlertKey,Severity,Type,Summary)
			#logdate=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())
			#logfile.write(logdate+" "+strs+"\n")
			#logfile.flush()
		else:
			Severity=severity
			Type="1"
			#Summary="DefineCount:"+definenumber+",RealCount:"+line
			Summary=agent_hostname+"进程"+procname+"实例数量异常,应该为"+definenumber+",实际为"+line
			strs=lc_libs.lc_gen_alert(agent_hostname,Node,AlertGroup,AlertKey,Severity,Type,Summary)
			print strs
			db.update_eventsdb(events_table,agent_hostname,Node,AlertGroup,AlertKey,Severity,Type,Summary)
			#logdate=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())
			#logfile.write(logdate+" "+strs+"\n")
			#logfile.flush()
		#数据入库
		#hdb.insert_historydb(Node,procname,Summary)
		datas=[
			{"Node":Node,"Name":procname,"Value":Summary,"Time":int(time.time())}
			]
		db.insertDatas(history_table,datas)
	except Exception:
		excinfo=traceback.format_exc()
		print excinfo
		logdate=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())
		logfile.write(logdate+" :\n"+excinfo+"\n")
		logfile.flush()
		time.sleep(1)


if __name__ == "__main__":


	lchome=lc_libs.lc_getlchome()
	scname=sys.argv[0].split('.')[0]
	logfilename=lchome+'/log/'+scname+'.log'
	confname=lchome+'/conf/'+scname+'.conf'
	
	today=time.strftime('%Y%m%d',time.localtime())
	os.system("mv %s %s.%s" % (logfilename,logfilename,today))
	
	logfile=file(logfilename,'a')
	logdate=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())
	logfile.write(logdate+" Started.\n")
	logfile.flush()
	
	agent_hostname=lc_libs.lc_gethostname()
	

	
	#初始化events数据库
	#dbfile="../db/lc_events.db"
	#tablename="lc_events"
	#tablefield="Node varchar(32),AlertGroup varchar(64),AlertKey varchar(64),Severity varchar(8),Type varchar(8),LastOccurrence varchar(32),Summary varchar(256)"
	#db=lc_libs.lc_dboperater(dbfile,tablename,tablefield)
	#db.create_db()
	#db.close_db()
	
	#初始化lc_history数据库
	#thismonth=time.strftime('%Y%m',time.localtime())
	#dbfile="../db/lc_history_"+thismonth+".db"
	#dbfile="../db/lc_history.db"
	#tablename="lc_process"
	#tablefield="Node varchar(32),Name varchar(64),Value varchar(64),NTime varchar(32)"
	#hdb=lc_libs.lc_dboperater(dbfile,tablename,tablefield)
	#hdb.create_db()
	#hdb.insert_historydb(Node,Name,Value)


	try:
		#初始化数据库
		
		db=lc_libs.lc_mongodb_operater('127.0.0.1',27017)

		db.connect()
		Conn=db.getConn()
		if Conn==None:
			print "Can not Connect to the MondbDB"
			exit(0)
		
		events_dbname='lc_events'
		events_tablename='lc_events'
		
		history_dbname='lc_history'
		history_tablename="lc_process"
		
		events_db=db.useDatabase(events_dbname)
		events_table=db.UseCollection(events_db,events_tablename)
		
		history_db=db.useDatabase(history_dbname)
		history_table=db.UseCollection(history_db,history_tablename)

	except Exception:
		excinfo=traceback.format_exc()
		logdate=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())
		logfile.write(logdate+" 初始化数据库失败:\n"+excinfo+"\n")
		logfile.flush()
		exit(0)

	#主程序
	MonitorList = []
	
	for lines in open(confname):
		if lines[0] == '#' or lines.split("\t")[0] not in (agent_hostname,'default'):
			continue

		line=lines.strip('\r\n').split('\t')
		dconf={}
		
		#dconf['agent_hostname']=line[0]
		dconf['alertgroup']=line[1]
		dconf['procname']=line[2]
		dconf['exceptword']=line[3]
		dconf['exactly']=line[4]
		dconf['definenumber']=line[5]
		dconf['operator']=line[6]
		dconf['interval']=line[7]
		dconf['severity']=line[8]
		dconf['schedule']=line[9]
		dconf['holiday']=line[10]
	
		MonitorList.append(dconf)

	usedtime=0
	
	while True:
		time.sleep(1)
		realtime=int(time.time())
		
		logdate=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())
		holiday_state = lc_libs.lc_isholiday()
		
		threads = []
		for i in MonitorList:
			if int(i['holiday']) == 1 and holiday_state == True:
				continue
				
			time_state = lc_libs.lc_schedule(i['schedule'])
			if time_state == False:
				continue
			
			flag=realtime%int(i['interval'])
			#print flag
			if flag<=usedtime:
				#print i
				t = threading.Thread(target=func,args=(i,))
				threads.append(t)
		
		for t in threads:
			t.start()
			while True:
				#print len(threading.enumerate())
				time.sleep(0.01)
				if(len(threading.enumerate()) < 100):
					break

		#排除执行程序导致的时间差
		usedtime=int(time.time())-realtime
		#print "usedtime:%s" % usedtime
